const ajax = require('../../../utils/ajax/ajax.js')
Page({
    data: {
        userName: '',
        grade: 0,
        avatar: '',
        friendList: [],
        teamLoading: true,
        pages: 1
    },
    onLoad: function (options) {
        const { avatar, grade, nickName } = options

        this.showMyInfo(avatar, grade, nickName)
        this.getMyFriends()
    },
    showMyInfo(avatar, grade, nickName) {
        // ['黑金1', '铂金2', '黄金3']
        this.setData({
            userName: nickName,
            avatar: avatar,
            grade: Number(grade)
        })
    },
    getMyFriends() {
        const that = this
        ajax.post({
            url: '/wx/getMyFriends',
            data: {
                page: 1,
                limit: 10
            },
            success(res) {
                that.setData({
                    friendList: res.data.list
                })
            }
        })
    },

    onReachBottom () {
        (this.data.pages) ++;
        this.reachBottomTeam()
    },
    
    reachBottomTeam() {
        const that = this
        ajax.post({
            url: '/wx/getMyFriends',
            data: {
                page: that.data.pages,
                limit: 10
            },
            success(res) {
                let friendList = res.data.list
                if (friendList.length === 0) {
                    that.setData({
                        teamLoading: true
                    })
                    return
                }
                
                if (that.data.pages === 1) {
                    that.setData({
                        friendList: friendList
                    })
                    return
                }

                if (that.data.pages !== 1) {
                    let newList = that.data.friendList.concat(friendList)
                    that.setData({
                        friendList: newList
                    })
                    return
                }
            }
        })
    },
    // 下拉刷新
    onPullDownRefresh() {
        this.getMyFriends()
        wx.stopPullDownRefresh()
    }
})